import 'dart:io';
import 'package:flutter/material.dart';

///
/// 底部弹窗框架
///
class QEmptySheet extends Dialog {
  final BuildContext buildContext;
  final Widget childWidget;
  final Function? backCallBack;

  const QEmptySheet({
    Key? key,
    required this.buildContext,
    required this.childWidget,
    this.backCallBack,
  }) : super(key: key);

  /// 取消对话框
  static hide(BuildContext context) {
    Navigator.of(context).pop();
  }

  /// 显示对话框
  void show() {
    showModalBottomSheet(
      context: buildContext,
      isScrollControlled: true,
      backgroundColor: Colors.transparent,
      builder: (BuildContext context) {
        return _BottomContent(
          childWidget: childWidget,
          backCallBack: backCallBack,
        );
      },
    );
  }
}

class _BottomContent extends StatefulWidget {
  final Widget? childWidget;
  final Function? backCallBack;

  const _BottomContent({
    Key? key,
    this.childWidget,
    this.backCallBack,
  }) : super(key: key);

  @override
  _BottomContentState createState() => _BottomContentState();
}

class _BottomContentState extends State<_BottomContent> {
  /// 拦截返回键
  Future<bool> _requestPop() {
    return Future.value(false);
  }

  @override
  Widget build(BuildContext context) {
    // TODO: implement build
    return WillPopScope(
      onWillPop: _requestPop,
      child: GestureDetector(
        onTap: () {
          Navigator.pop(context);
          if (widget.backCallBack != null) {
            widget.backCallBack!.call();
          }
        },
        child: Container(
          height: double.infinity,
          width: double.infinity,
          color: Colors.transparent,
          child: Align(
            alignment: Alignment.bottomCenter,
            child: Container(
              height: Platform.isAndroid
                  ? 703 / 2
                  : 703 / 2 + MediaQuery.of(context).padding.bottom,
              padding: EdgeInsets.fromLTRB(
                  29 / 2,
                  51 / 2,
                  29 / 2,
                  Platform.isAndroid
                      ? 11
                      : 11 + MediaQuery.of(context).padding.bottom),
              decoration: const BoxDecoration(
                  borderRadius: BorderRadius.only(
                      topRight: Radius.circular(7),
                      topLeft: Radius.circular(7)),
                  color: Colors.white),
              child: Column(
                mainAxisAlignment: MainAxisAlignment.spaceBetween,
                crossAxisAlignment: CrossAxisAlignment.center,
                children: [widget.childWidget!],
              ),
            ),
          ),
        ),
      ),
    );
  }
}
